package com.mfz.common.definition.symbol

/**
 * 整个程序使用的类型字段。
 *
 * create by 2019/9/22.
 * @author udbjqr
 */
class Type(override val id: Int, override val name: String) : Symbol() {
	companion object : Iterable<Type> {
		//保存注册到自己的所有对象
		private val arrays = ArrayList<Type>()

		val UNKNOWN = Type(-1, "未知类型")


		fun findById(value: Int): Type {
			for (element in arrays) if (element.id == value) return element
			return UNKNOWN
		}

		fun findByName(value: String): Type {
			for (element in arrays) if (element.name == value) return element
			return UNKNOWN
		}

		override fun iterator(): Iterator<Type> {
			return arrays.iterator()
		}
	}

	init {
		if (checkSymbolExists(arrays, this)) throw SymbolAlreadyExistsException(this)
		arrays.add(this)
	}


}
